
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 07/16/2011 23:27:49
-- Generated from EDMX file: E:\Win7\Users\Yvonne\Documents\Visual Studio 2010\Projects\DDDFramework\Persistance\Context\SampleModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Sample];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserGroup_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserGroup] DROP CONSTRAINT [FK_UserGroup_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserGroup_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserGroup] DROP CONSTRAINT [FK_UserGroup_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_UserMBlog]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Items_MBlog] DROP CONSTRAINT [FK_UserMBlog];
GO
IF OBJECT_ID(N'[dbo].[FK_UserComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comments] DROP CONSTRAINT [FK_UserComment];
GO
IF OBJECT_ID(N'[dbo].[FK_ItemComment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comments] DROP CONSTRAINT [FK_ItemComment];
GO
IF OBJECT_ID(N'[dbo].[FK_User_inherits_Item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Items_User] DROP CONSTRAINT [FK_User_inherits_Item];
GO
IF OBJECT_ID(N'[dbo].[FK_MBlog_inherits_Item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Items_MBlog] DROP CONSTRAINT [FK_MBlog_inherits_Item];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Groups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Groups];
GO
IF OBJECT_ID(N'[dbo].[Comments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comments];
GO
IF OBJECT_ID(N'[dbo].[Items]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Items];
GO
IF OBJECT_ID(N'[dbo].[Items_User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Items_User];
GO
IF OBJECT_ID(N'[dbo].[Items_MBlog]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Items_MBlog];
GO
IF OBJECT_ID(N'[dbo].[UserGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserGroup];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [ID] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Comments'
CREATE TABLE [dbo].[Comments] (
    [ID] uniqueidentifier  NOT NULL,
    [Body] nvarchar(max)  NULL,
    [Title] nvarchar(max)  NULL,
    [CreateTime] datetime  NOT NULL,
    [AuthorID] uniqueidentifier  NOT NULL,
    [Type] nvarchar(max)  NOT NULL,
    [ItemID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Items'
CREATE TABLE [dbo].[Items] (
    [ID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Items_User'
CREATE TABLE [dbo].[Items_User] (
    [Name] nvarchar(max)  NOT NULL,
    [Address] nvarchar(max)  NULL,
    [ID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Items_MBlog'
CREATE TABLE [dbo].[Items_MBlog] (
    [Body] nvarchar(max)  NOT NULL,
    [CreateTime] datetime  NOT NULL,
    [Status] int  NOT NULL,
    [UserID] uniqueidentifier  NOT NULL,
    [ID] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'UserGroup'
CREATE TABLE [dbo].[UserGroup] (
    [Members_ID] uniqueidentifier  NOT NULL,
    [Groups_ID] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [PK_Comments]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Items'
ALTER TABLE [dbo].[Items]
ADD CONSTRAINT [PK_Items]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Items_User'
ALTER TABLE [dbo].[Items_User]
ADD CONSTRAINT [PK_Items_User]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Items_MBlog'
ALTER TABLE [dbo].[Items_MBlog]
ADD CONSTRAINT [PK_Items_MBlog]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [Members_ID], [Groups_ID] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [PK_UserGroup]
    PRIMARY KEY NONCLUSTERED ([Members_ID], [Groups_ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Members_ID] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [FK_UserGroup_User]
    FOREIGN KEY ([Members_ID])
    REFERENCES [dbo].[Items_User]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_ID] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [FK_UserGroup_Group]
    FOREIGN KEY ([Groups_ID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserGroup_Group'
CREATE INDEX [IX_FK_UserGroup_Group]
ON [dbo].[UserGroup]
    ([Groups_ID]);
GO

-- Creating foreign key on [UserID] in table 'Items_MBlog'
ALTER TABLE [dbo].[Items_MBlog]
ADD CONSTRAINT [FK_UserMBlog]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[Items_User]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserMBlog'
CREATE INDEX [IX_FK_UserMBlog]
ON [dbo].[Items_MBlog]
    ([UserID]);
GO

-- Creating foreign key on [AuthorID] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [FK_UserComment]
    FOREIGN KEY ([AuthorID])
    REFERENCES [dbo].[Items_User]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserComment'
CREATE INDEX [IX_FK_UserComment]
ON [dbo].[Comments]
    ([AuthorID]);
GO

-- Creating foreign key on [ItemID] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [FK_ItemComment]
    FOREIGN KEY ([ItemID])
    REFERENCES [dbo].[Items]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ItemComment'
CREATE INDEX [IX_FK_ItemComment]
ON [dbo].[Comments]
    ([ItemID]);
GO

-- Creating foreign key on [ID] in table 'Items_User'
ALTER TABLE [dbo].[Items_User]
ADD CONSTRAINT [FK_User_inherits_Item]
    FOREIGN KEY ([ID])
    REFERENCES [dbo].[Items]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ID] in table 'Items_MBlog'
ALTER TABLE [dbo].[Items_MBlog]
ADD CONSTRAINT [FK_MBlog_inherits_Item]
    FOREIGN KEY ([ID])
    REFERENCES [dbo].[Items]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------